<?php
/**
 * 《二维码》实现类
 *
 * @author 孙晓晔
 * @version $Id: Code.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/InfoUtil.class.php');

class Qr extends InfoUtil {
	public $tbl = Constant::tbl_qr;
	
	const TYPE_TXT = 1;
	const TYPE_URL = 2;
	const TYPE_TEL = 3;
	const TYPE_SMS = 4;
	const TYPE_EMAIL = 5;
	const TYPE_VCARD = 6;
	const TYPE_WIFI = 7;
	const TYPE_GEO = 8;
	
	public $type = array(
		// '-' => '请选择',
		'1' => '文本',
		'2' => '网址',
		'3' => '电话',
		'4' => '短信',
		'5' => '邮件',
		'6' => '名片',
		'7' => 'WiFi',
		'8' => '位置',

		'9' => '其它',
	);
	
	public $level = array(
		// '-' => '请选择',
		'L' => '7%',
		'M' => '15%',
		'Q' => '25%',
		'H' => '30%',
	);
	
	public $safe = array(
			'WPA' => 'WPA/WPA2',
			'WEP' => 'WEP',
			'3' => '无加密',
	);
	
	public $status = array(
		'-' => '请选择',
		'1' => '等待审核',
		'2' => '正在审核',
		'3' => '审核通过',

		'9' => '其它',
	);
	
	public $orderby = array(
		'-' => '请选择',
		'oper' => '员工',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct();
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('type_option', $this->type);
		$controller->assign_by_ref('level_option', $this->level);
		
		$controller->assign_by_ref('safe_option', $this->safe);
	}
	
	function txt($controller, $request) {
		$this->pretreat($controller);
		
		$controller->display($request, 'info/qr/txt');
		return true;
	}
	
	function content($controller, $request) {
		$this->pretreat($controller);
		
		$type = $request['type'];
		$content = addslashes(trim($request['content']));
		
		debug(print_r($request, true));
		
		$this->_insert($type, array(), $content);
	}
	
	function _insert($type, $a, $content = '') {
		// $dd = date('Y-m-d', strtotime("-1 WEEK"));

		$emp = $_SESSION['id'];
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		if (is_array($a)) {
			$a['qr_type'] = $type;
			$a['qr_content'] = $content;
		} else {
			$a->qr_type = $type;
			$a->qr_content = $content;
		}		
		
		$json = addslashes(json_encode($a));
		$md5 = md5($json);
		
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE md5 = '%s'";
		$sql = sprintf($fmt, $this->tbl, $md5);
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			echo $row['id'];
		} else {
			$fmt = "INSERT INTO %s(office, dept, type, json, content, md5, emp, oper, createtime, modifytime) VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
			$sql = sprintf($fmt, $this->tbl, $office, $dept, $type, $json, $content, $md5, $emp, $_SESSION['id']);
			debug($sql);
			$db->query($sql);
			
			echo $db->insert_id();
		}
	}
	
	function img($controller, $request) {
		require_once(APP_LIB . 'phpqrcode/qrlib.php');
		
		$value = $request['value'];
		if (empty($value)) {
			$value = "这是一个测试信息，有任何疑问请联系孙晓晔";
		}
		
		$errorCorrectionLevel = 'M';
		$matrixPointSize = 3;
		
		return QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize);
	}
	
	function url($controller, $request) {
		$this->pretreat($controller);
		
		$controller->display($request, 'info/qr/url');
		return true;
	}
	
	function call($controller, $request) {
		$this->pretreat($controller);
		
		$controller->display($request, 'info/qr/call');
		return true;
	}
	
	function sms($controller, $request) {
		$this->pretreat($controller);
		
		$controller->display($request, 'info/qr/sms');
		return true;
	}
	
	function smsPost($controller, $request) {
		$this->pretreat($controller);
		
		$type = $request['type'];
		
		$a = array();
		$a['to'] = trim($request['to']);
		$a['content'] = trim($request['content']);
		
		$this->_insert($type, $a, "");
	}
	
	function email($controller, $request) {
		$this->pretreat($controller);
		
		$controller->display($request, 'info/qr/email');
		return true;
	}
	
	function emailPost($controller, $request) {
		$this->pretreat($controller);
		
		$type = $request['type'];
		
		$a = array();
		$a['to'] = trim($request['to']);
		$a['title'] = trim($request['title']);
		$a['content'] = trim($request['content']);
		
		$this->_insert($type, $a, "");
	}
	
	function vcard($controller, $request) {
		$this->pretreat($controller);
		
		$controller->display($request, 'info/qr/vcard');
		return true;
	}
	
	function vcardPost($controller, $request) {
		$this->pretreat($controller);
		
		$type = $request['type'];
		
		$a = array();
		$a['name'] = trim($request['name']);
		$a['bday'] = trim($request['bday']);
		
		$a['job'] = trim($request['job']);
		$a['org'] = trim($request['org']);
		$a['url'] = trim($request['url']);
		$a['adr'] = trim($request['adr']);
		$a['tel'] = trim($request['tel']);
		$a['fax'] = trim($request['fax']);
		$a['geo'] = trim($request['geo']);
		
		$a['cell'] = trim($request['cell']);		
		$a['email'] = trim($request['email']);
		
		$a['content'] = trim($request['content']);
		
		$this->_insert($type, $a, "");
	}
	
	function wifi($controller, $request) {
		$this->pretreat($controller);
				
		$controller->display($request, 'info/qr/wifi');
		return true;
	}
	
	function wifiPost($controller, $request) {
		$this->pretreat($controller);
		
		$type = $request['type'];
		
		$a = array();
		$a['ssid'] = trim($request['ssid']);
		$a['password'] = trim($request['password']);
		$a['safe'] = trim($request['safe']);
		
		$this->_insert($type, $a, "");
	}
	
	function geo($controller, $request) {
		$this->pretreat($controller);
		
		$controller->display($request, 'info/qr/geo');
		return true;
	}
	
	function find($controller, $request) {
		$this->pretreat($controller);
		
		$controller->display($request, 'info/qr/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}
	
// 		if ($this->_audit('155') && $this->_audit('156')) {
// 			return " visible = 1";
// 		}
	
// 		if ($this->_audit('155')) {
// 			return " visible = 1 AND office = '1'";
// 		}
	
// 		if ($this->_audit('156')) {
// 			return " visible = 1 AND office = '2'";
// 		}
	
		$emp = $_SESSION['id'];
	
		return sprintf(" visible = 1 AND (emp = '%s' OR oper = '%s')", $emp, $emp);
	}

	function _where($request) {		
		$name_s = $request['name_s'];

		$my = $this->_my();
		
		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
		
		$where .= $this->_where_x("type", $request['type_s']);

		if (!empty($name_s)) {
			$where .= " AND (name LIKE '%" . $name_s . "%' OR content '%" . $name_s . "%')";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'info/qr/list');
		return true;
	}
	
	function gen($controller, $request) {
		$this->pretreat($controller);
	
		$controller->display($request, 'info/qr/gen');
		return true;
	}
	
	function genPost($controller, $request) {
		$this->pretreat($controller);
	
		$type = $request['type'];
		$content = addslashes(trim($request['content']));
	
		$content_x = $request['content_x'];
		$a = json_decode($content_x);
	
		$this->_insert($type, $a, $content);
	}
	
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/qr/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$type = $request['type'];
		$level = $request['level'];
		$size = $request['size'];

		$name = addslashes($request['name']);
		$content = addslashes(trim($request['content']));
				
		$content_x = $request['content_x'];		
		$a = json_decode($content_x);
		
		// $a = array();
		$a->qr_type = $type;
		$a->qr_content = $content;		
		
		$json = addslashes(json_encode($a));
		$md5 = md5($json);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, name, type, level, size, json, content, md5, emp, oper, createtime, modifytime)";
		$fmt .=" VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $name, $type, $level, $size, $json, $content, $md5, $emp, $_SESSION['id']);
		debug($sql);
		$db->query($sql);

		$request['id_s'] = $db->insert_id();
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/qr/edit');
		return true;
	}

	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$type = $request['type'];
		$level = $request['level'];
		$size = $request['size'];

		$name = addslashes($request['name']);
		$content = addslashes(trim($request['content']));
		
		$content_x = $request['content_x'];		
		$a = json_decode($content_x);
		
		// $a = array();
		$a->qr_type = $type;
		$a->qr_content = $content;
		
		$json = addslashes(json_encode($a));
		$md5 = md5($json);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', type = '%s', level = '%s', size = '%s', name = '%s', json = '%s', content = '%s', md5 = '%s', emp = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $type, $level, $size, $name, $json, $content, $md5, $emp, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'info/qr/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/qr/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
	
		$row['type_t'] = $this->type[$row['type']];
		$row['level_t'] = $this->level[$row['level']];
		
		$row['j'] = json_decode($row['json']);
		
		if ($row['type'] == '7') {
			$row['j']->safe_t = $this->safe[$row['j']->safe];
		}
	
		$row = $this->_view_t($row);
	
		return $row;
	}
	
	function code($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
		
		$s = $this->_value($controller, $request, $row);
		
		$errorCorrectionLevel = $row['level'];
		$matrixPointSize = $row['size'];
		
		require_once(APP_LIB . 'phpqrcode/qrlib.php');
		
		return QRcode::png($s, false, $errorCorrectionLevel, $matrixPointSize);
	}
	
	function _value($controller, $request, $row) {
		$ret = "";
		
		$type = $row['type'];
		switch($type) {
			case self::TYPE_TXT:
			case self::TYPE_URL:				
				$ret = $row['content'];
				break;
			case self::TYPE_TEL:
				$ret = sprintf("TEL:%s", $row['content']);
				break;
			case self::TYPE_SMS:
				$j = json_decode($row['json']);
				
				$ret = sprintf("SMSTO:%s:%s", $j->{'to'}, $j->{'content'});
				break;
			case self::TYPE_EMAIL:
				$j = json_decode($row['json']);
				
				$ret = sprintf("MATMSG:;TO:%s;SUB:%s;BODY:%s;", $j->{'to'}, $j->{'title'}, $j->{'content'});
				break;
			case self::TYPE_VCARD:
				$row['j'] = json_decode($row['json']);
			
				$controller->assign_by_ref('result', $row);
			
				$ret = $this->_tj_html($controller, $request, 'info/qr/_vcard');
				break;
			case self::TYPE_WIFI:
				$row['j'] = json_decode($row['json']);
				
				if ($j->{'safe'} == '3') {
					$ret = sprintf("WIFI:T:%s;S:%s;P:%s;;", "", $j->{'ssid'}, "");
				} else {		
					$ret = sprintf("WIFI:T:%s;S:%s;P:%s;;", $j->{'safe'}, $j->{'ssid'}, $j->{'password'});
				}
				
				break;
			case self::TYPE_GEO:
				$ret = sprintf("geo:%s", $row['content']);
				break;
			default:
				$ret = "sunxiaoye.";
		}
		
		debug(print_r($ret, true));
		return $ret;
	}	
}
?>